# 标准库
import itertools
from collections import deque
from collections import Counter
import collections
from sys import modules
from warnings import resetwarnings
import numpy as np
import random
from random import *
import time
t_local = time.localtime()
t_UTC = time.gmtime()
# print(t_local) #本地时间
# print(t_UTC) #UTC统一时间
# print(time.ctime()) #UTC统一时间

# print(time.time()) #返回自纪元以来的秒数，记录sleep
# print(time.perf_counter()) #随意选取一个时间点，记录现在时间到该时间点的间隔秒数，记录sleep
# print(time.process_time()) #随意选取一个时间点，记录现在时间到该时间点的间隔秒数，不记录sleep
# perf_counter()精度较time()更高一些


# t_1_start=time.time()
# t_2_start=time.perf_counter()
# t_3_start=time.process_time()

# res =0
# for i in range(1000000):
#     res+=i

# time.sleep(2)
# t_1_end =time.time()
# t_2_end = time.perf_counter()
# t_3_end = time.process_time()

# print("time方法:{:.3f}秒".format(t_1_end-t_1_start))
# print("time_perf_counter方法:{:.3f}秒".format(t_2_end-t_2_start))
# print("time_perf_counter方法:{:.3f}秒".format(t_3_end-t_3_start))

# 格式化
# lctime = time.localtime()
# print(time.strftime("%Y-%m-%d %A %H:%M:%S",lctime))


# 1.random
'''
随机种子
1.相同种子会产生相同的随机
2.如果不设置随机种子，以系统当前时间为默认值
'''
# seed(10)
# print(random())
# seed(10)
# print(random())

# numbers=[randint(1,10) for i in range(10)]   #randint(a,b) 产生[a,b]随机数
# print(numbers)


# numbers= [randrange(10) for i in range(10)]   #产生(0,a)之间的随机数
# print(numbers)

# numbers= [randrange(0,10,2) for i in range(10)]  #产生[a,b]之间以step为步长
# print(numbers)


# 3.产生随机浮点数
# numbers=[random() for i in range(10)]  #产生(0.0,1.0)之间的随机浮点数
# print(numbers)

# numbers = [uniform(2.1,3.5) for i in range(10)] #产生[a,b]之间的随机浮点数
# print(numbers)


# 4.序列用函数
# 1.choice(seq) 从序列类型中随机返回一个元素
# print(choice(['A','B',"C"]))
# print(choice(['Python']))

# choices(seq,weights=None,k)  对序列进行k次重复采样，可设置权重
# print(choices(['A','B','C','D'],k=4))
# print(choices(['A','B','C','D'],[10,20,30,1],k=4))

# shuffle(seq) 将序列类型中元素随机排列，返回打乱后的序列
# numbers=['One','Two','Three','Four','Five']
# shuffle(numbers)
# print(numbers)

# sample(pop,k) 从pop类型中随机选取k个元素，以列表类型返回
# print(sample([10,20,30,40,50],k=3))

# 5.概率分布
'''
gauss(mean,std)--生产一个符合高斯分布的随机数
'''

# numbers= gauss(0,1)
# print(numbers)

# import matplotlib.pyplot as plt
# res=[gauss(0,1) for i in range(1000)]

# plt.hist(res,bins=1000)
# plt.show()

'''
使用random库实现简单的微信红包分配
'''

# def red_packet(total,num):
#     for i in range(1,num):
#         per = random.uniform(0.01,total/(num-i+1)*2) #保证每个人获得红包的期望是total/Num
#         total=total-per
#         print("第{}位红包金额:{:.2f}元".format(i,per))
#     else:
#         print("第{}位红包金额:{:.2f}元".format(num,total))

# red_packet(10,5)


# 重复发10w次红包，统计每个位置的平均值（约等于期望2）
# def red_packet(total, num):
#     ls = []
#     for i in range(1, num):
#         # 保证每个人获得红包的期望是total/Num
#         per = round(uniform(0.01, total/(num-i+1)*2), 2)
#         ls.append(per)
#         total = total - per
#     else:
#         ls.append(total)
#     return ls

# res = []
# for i in range(100000):
#     ls = red_packet(10, 5)
#     res.append(ls)
# res =np.array(res)
# print(np.mean(res,axis=0))


'''
生产4位由数字和英文字母构成的验证码
'''
# import string
# print(string.digits)
# print(string.ascii_letters)

# s=string.digits+string.ascii_letters
# v=sample(s,4)
# print(v)
# print(','.join(v))


'''
2.Collections 容器数据类型
'''
# nametuple-具名元组
'''
点的坐标,仅仅看数据，很难知道表达的是一个点的坐标
p=(1,2)
'''
# 构建一个新的元组子类
# 定义方法如下：typeName是元组名字，field_names是域名
# collections.namedtuple(typename,field_names,*,rename=False,defaults=None,module=None)

Point = collections.namedtuple("Point", ["x", "y"])
p = Point(1, y=2)
# print(p)
# #调用属性
# print(p.x)
# print(p.y)

# 获取元组的性质
# print(p[0])
# print(p[1])


# 确定是元组的子类
# print(isinstance(p,tuple))


'''
模拟扑克牌
'''
Card = collections.namedtuple("Card", ["rank", "suit"])
ranks = [str(n) for n in range(2, 11)] + list("JQKA")
suits = "spades diamonds clubs hearts".split()
# print("ranks", ranks)
# print("suis ts", suits)
cards = [Card(rank, suit) for rank in ranks
         for suit in suits]

# print(cards)

# 洗牌
# shuffle(cards)

# 随机抽一张牌
# print(choice(cards))

# # 随机抽多张
# sample(cards, k=5)


'''
计时器工具 Counter   字典子类
'''
s = "牛奶奶找刘奶奶买牛奶"
colors = ["red", "blue", "red", "green", "blue", "blue"]
ent_str = Counter(s)
ent_col = Counter(colors)
# print(ent_str)
# print(ent_col)


# print(isinstance(Counter(),dict))

# 最常见的统计-most_common(n) 提供n个频率最高的元素和计数

# print(ent_str.most_common(2))

# print(list(ent_str.elements()))

# 加减操作
# c=Counter(a=3,b=1)
# d=Counter(a=1,b=2)
# print(c+d)


'''
从一副牌中抽取10张，大于10的比例有少
'''
# cards= collections.Counter(tens=16,low_cards=36)
# seen=sample(list(cards.elements()),k=20)
# print(seen)
# print(seen.count('tens')/20)


# 双向队列  deque

d = deque("cde")
# print(d)

# d.append("1")  # 右侧增加

# d.appendleft("2")  # 左侧增加

# print(d)

# d.pop()  # 右侧删除
# d.popleft()  # 左侧删除

'''
itertools库---迭代器
'''
# 笛卡尔积
# for i in itertools.product('ABC','01'):
#     print(i)

# for i in itertools.product('ABC',repeat=3):
#     print(i)

# 排列
# for i in itertools.permutations("ABCD",3): #3是排列的长度
#     print(i)
# for i in itertools.permutations(range(3)):
#     print(i)

# 组合
# for i in itertools.combinations('ABCD',2): #2是组合的长度
#     print(i)

# for i in itertools.combinations(range(4),3):
#     print(i)

# 元素可重复组合
# for i in itertools.combinations_with_replacement("ABC",2): #2是组合的长度
#     print(i)
# print("-----")
# for i in itertools.product('ABC',repeat=2):
#     print(i)

# 2、拉链
# zip--短拉链
# for i in zip('ABC','012','xyz'):
#     print(i)

# for i in zip('ABC','0123456'):
#     print(i)


#zip--长拉链
# for i in itertools.zip_longest('ABC','012345'):
#     print(i)
# for i in itertools.zip_longest('ABC','012345',fillvalue="?"):
#     print(i)


#3.无穷迭代器
#count(start=0,step=1)-计数，创建一个迭代器，它从start值开始，返回均匀间隔的值
# for i in itertools.count(10):
#     print(i)


#cycle(iterable)-循环
#创建一个迭代器，返回iterable中所有元素，无限重复
# for i in itertools.cycle("ABC"):
#     print(i)


#repeat(object,[,times])---重复
'''
创建一个迭代器，不能重复object，除非设定参数times，否则将无限重复
'''
# for i in itertools.repeat(10,3):
#     print(i)


#其他--锁链
#chain--锁链  把一组迭代对象串联起来，形成一个更大的迭代器
# for i in itertools.chain("ABC",[1,2,3]):
#     print(i)

#枚举 enumerate- 产出由两个元素组成的元组，结构是(index,item),其中index是从start开始，item从iterable中取
# for i in enumerate("Python",start=1):
#     print(i)


#groupby分组
'''
创建一个迭代器，按照key指定方式，返回iterable中连续的键和值
一般来说，要预先对数据进行排序
key为None默认把连续重复元素分组
'''
# for key,group in itertools.groupby("AAAABBBCCDAABBB"):
#     print(key,list(group))

# animals=["duck","rat","eagle","bear","bat","dolphin","shark","lion"]
# animals.sort(key=len)
# print(animals)
# for key,group in itertools.groupby(animals,key=len):
#     print(key,list(group))

# animals=["duck","rat","eagle","bear","bat","dolphin","shark","lion"]
# animals.sort(key=lambda x:x[0])
# print(animals)
# for key,group in itertools.groupby(animals,lambda x:x[0]):
#     print(key,list(group))